package com.lvyq.myplayer.adapter.singer

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.parcelable.SongInfoData
import com.lvyq.myplayer.widget.LoadMoreView
import com.lvyq.myplayer.widget.LoadNoMoreView
import com.lvyq.myplayer.widget.common.song.SongItemPlainTextView

/**
 * 文件描述：SingerSongAdapter
 * 作者：lvyq
 * 创建时间：2025-04-25
 * 修改时间：2025-04-25
 */
class SingerSongAdapter(var context: Context?,
                        private val itemClickListener: OnItemClickListener
): RecyclerView.Adapter<SingerSongAdapter.SingerSongViewHolder>() {
    lateinit var song:TextView
    lateinit var singer:TextView

    companion object{
        val LOAD_INIT = 0 //初始化
        val LOAD_MORE = 1 //加载更多
        val LOAD_END = 2
    }
    // 初始化状态标记
    private var isDataInitialized = false
    //数据定义
    var list = ArrayList<SongInfoData>()
    //总条数 默认一条-暂无数据条目
    var totalCount = Int.MAX_VALUE

    interface OnItemClickListener {
        fun onItemClick(position: Int, data: SongInfoData)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SingerSongViewHolder {
        if (viewType== LOAD_END){
            return SingerSongViewHolder(LoadNoMoreView(parent.context))
        }
        if (viewType== LOAD_MORE){
            //最后一条，返回进度条
            return SingerSongViewHolder(LoadMoreView(parent.context))
        }else{
            //普通条目
            return SingerSongViewHolder(SongItemPlainTextView(parent.context))
        }

    }

    /**
     * 返回类型，用于显示判断
     */

    override fun getItemViewType(position: Int): Int {
        // 判断是否为最后一个条目（需加载）
        if (position==totalCount) return LOAD_END
        if (position == list.size) return LOAD_MORE
        return LOAD_INIT
    }

    override fun getItemCount(): Int {
        return list.size+1
    }

    override fun onBindViewHolder(holder: SingerSongViewHolder, position: Int) {
        if (position==list.size) return
        val data =list[position]
        if (getItemViewType(position)== LOAD_INIT){
            val item = holder.itemView as SongItemPlainTextView
            item.setData(data)
            item.setOnClickListener{
                itemClickListener.onItemClick(position,data)
            }

        }

    }

    // 加载更多数据
    fun loadMore(lst: ArrayList<SongInfoData>, total: Int) {
        list.addAll(lst)
        totalCount=total
        notifyDataSetChanged()
    }
    // 初始化
    fun initData(lst: ArrayList<SongInfoData>, total: Int) {
        this.list.clear()
        this.list.addAll(lst)
        this.totalCount = total
        isDataInitialized = true
        notifyDataSetChanged()
    }


    inner class SingerSongViewHolder(view: View) : RecyclerView.ViewHolder(view) {

    }

}


